<?php
class ZoneForm extends Form{
	function ZoneForm(){
		Form::Form('ZoneForm');
		CGlobal::$website_title='Quản trị Nhóm danh mục';
	}

	function draw(){
								
		global $display;
		$this->beginForm();
		
		$all_zone_cats = array();
		$re = DB::query("SELECT c.id,  z.zoneid, c.name , c.status FROM (SELECT zoneid, catid FROM category_zone_cat) AS z LEFT JOIN category AS c ON c.id=z.catid ORDER BY position");
		
		if($re){
			while($cat = mysql_fetch_assoc($re)){
				if($cat['status'] == 'HOME'){
					$cat['name'].=' (HOME)';
				}
				elseif($cat['status'] == 'HIDE'){
					$cat['name'].=' (HIDE)';
				}
				
				$cat['link'] = Url::build_current(array('cmd'=>'edit','id'=>$cat['id']));
				
				$all_zone_cats[$cat['zoneid']][$cat['id']] = $cat;
			}
		}
		
		$sql="SELECT * FROM category_zone ORDER BY position";
		
		$re=DB::query($sql);
		$items=array();
		$sub_items=array();
		
		if($re){
			$last_id=0;
			while ($row=mysql_fetch_assoc($re)) {
				if(isset($all_zone_cats[$row['id']])){
					$row['cats_total']	=	count($all_zone_cats[$row['id']]);
					$row['cats']		=	$all_zone_cats[$row['id']];
				}
				else{
					$row['cats_total']	=	0;
					$row['cats']		=	array();
				}
				
				$row['edit_link']=Url::build_current(array('mod','cmd'=>'edit','id'=>$row['id']));
				
				if($row['status'] == 1){
					$row['status']='<b>Hiện</b> [ <a href="'.Url::build_current(array('mod','cmd'=>'hide','id'=>$row['id'])).'">Ẩn</a> ]';
				}
				elseif($row['status']==0){
					$row['status']='<b>Ẩn</b> [ <a href="'.Url::build_current(array('mod','cmd'=>'show','id'=>$row['id'])).'">Hiện</a> ]';
				}
				
				//Top cat
				if(!isset($sub_items[$row['id']]))
				$sub_items[$row['id']]=array();
				
				if($last_id){
					$items[$last_id]['down']='<a href="'.Url::build_current(array('mod','cmd'=>'move_down','id'=>$last_id)).'"><img src="style/images/admin/down_arrow_.gif" /></a>';
					$row['up']='<a href="'.Url::build_current(array('mod','cmd'=>'move_up','id'=>$row['id'])).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
				}
				else {
					$row['up']='';
				}
				$row['down']='';
				
				$items[$row['id']]=$row;
				$last_id = $row['id'];
			}
			
			if($last_id && $items[$last_id]['position']>1){
				$items[$last_id]['up']='<a href="'.Url::build_current(array('mod','cmd'=>'move_up','id'=>$last_id)).'"><img src="style/images/admin/up_arrow_.gif" /></a>';
			}
		}
		
		$display->add('items',$items);
		$display->add('hover','');
		$display->output('zone_list');
		$this->endForm();
	}				
}
?>